package timbap.visualizer.coreplugins;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import processing.core.PApplet;
import timbap.visualizer.api.model.PlaylistEntry;
import timbap.visualizer.api.model.RecordItem;

import java.util.ArrayList;
import java.util.List;

public class DefaultBrowsingModeControlImpl extends AbstractBrowsingController {
    
    private static final Log LOG = LogFactory.getLog(DefaultBrowsingModeControlImpl.class);
    
    private static final float SELECTION_SCALE_FACTOR_BOOST = 0.2f;
    
    @Override
    protected void updateRecordItems() {
        
    	int tRecordItemIndex = getContext().getRecordItemManager().getRecordItemCount() - 1;
        for (RecordItem tRecordItem: getContext().getRecordItemManager().getRecordItems()) {
            
            float tOffset = (float)tRecordItemIndex / getContext().getRecordItemManager().getRecordItemCount();
            float tRotation = getRotation() + tOffset;
            tRotation = tRotation - PApplet.floor(tRotation);
        
            tRecordItem.getTransformation().setRotation(0);
            tRecordItem.getTransformation().setXRotation(0);
            tRecordItem.getTransformation().setAlphaValue(1.0f);
            
            float zTranslation = 0;
            float tScaleFactor;
            if (tRotation > 0.5f) {
                tScaleFactor = PApplet.map(tRotation, 0.5f, 1, 0.75f, 0);
            } else {
                tScaleFactor = PApplet.map(tRotation, 0, 0.5f, 0, 0.75f);
            }
            if (tRecordItem.isSelected()) {
                tScaleFactor = tScaleFactor + SELECTION_SCALE_FACTOR_BOOST;
                zTranslation = 5;
            }
            tRecordItem.getTransformation().setScaleFactor(tScaleFactor);
            
            float tMappedRotation = PApplet.map(tRotation, 0, 1, -1.7f, 1.7f);
            float xTranslation = PApplet.sin(tMappedRotation) * 150;
            float yTranslation = PApplet.sin(1.7f - tMappedRotation) * -150;
            tRecordItem.getTransformation().setXTranslation(xTranslation);
            tRecordItem.getTransformation().setYTranslation(yTranslation);
            tRecordItem.getTransformation().setZTranslation(zTranslation);
            
            tRecordItemIndex--;
            
        }
        
    }
    
    

    @Override
    protected void loadNextPlaylistEntry(int pOldNextRecordItemIndex, int pNewNextRecordItemIndex, int pNextPlaylistIndex) {
        
        super.loadNextPlaylistEntry(pOldNextRecordItemIndex, pNewNextRecordItemIndex, pNextPlaylistIndex);
        
        PlaylistEntry tNextPlaylistEntry = getContext().getPlaylistManager().getPlaylistEntry(pNextPlaylistIndex);
        RecordItem tNextRecordItem = getContext().getRecordItemManager().getRecordItemAt(pNewNextRecordItemIndex);
        
        if (tNextRecordItem != null && tNextPlaylistEntry != null) {
            getContext().getRecordItemManager().setPlaylistEntry(tNextRecordItem, tNextPlaylistEntry);
        } else {
            LOG.error("Loading next playlist entry failed: Tried to load a null record item or playlist entry");
        }
        
    }

    @Override
    protected void resetPlaylistEntries(int pNewNextRecordItemIndex, int pOldNextRecordItemIndex, int[] pPlaylistIndices) {
        
    	super.resetPlaylistEntries(pNewNextRecordItemIndex, pOldNextRecordItemIndex, pPlaylistIndices);
    	
    	List<PlaylistEntry> tPlaylistEntries = new ArrayList<PlaylistEntry>();
        for (int pPlaylistIndice : pPlaylistIndices) {
            tPlaylistEntries.add(getContext().getPlaylistManager().getPlaylistEntry(pPlaylistIndice));
        }
    	getContext().getRecordItemManager().setPlaylistEntries(tPlaylistEntries);
    	
    }


}
